package com.netease.nim.demo.main.viewholder;

import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.netease.nim.demo.NimCache;
import com.netease.nim.demo.common.adapter.TViewHolder;
import com.netease.nim.demo.common.ui.imageview.HeadImageView;
import com.netease.nim.demo.contact.cache.TeamMemberInfoCache;
import com.netease.nim.demo.main.fragment.RecentContactsFragment;
import com.netease.nim.demo.main.helper.MessageHelper;
import com.netease.nim.demo.main.reminder.ReminderSettings;
import com.netease.nim.demo.session.emoji.MoonUtil;
import com.netease.nim.demo.util.TimeUtil;
import com.netease.nimlib.sdk.msg.constant.MsgStatusEnum;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.RecentContact;
import com.shwread.android.qysw10000243.R;

import logic.bean.TeamMemberX;
import logic.constants.Const;
import logic.util.ScreenUtil;

public abstract class RecentViewHolder extends TViewHolder implements OnClickListener {

    protected FrameLayout portraitPanel;

    protected HeadImageView imgHead;
    protected ImageView imgManger;

    protected TextView lblNickname;

    protected TextView lblMessage;

    protected TextView lblUnread;

    protected View unreadIndicator;

    protected TextView lblDatetime;

    // 消息发送错误状态标记，目前没有逻辑处理
    protected ImageView imgMsgStatus;

    protected RecentContact recent;

    protected View bottomLine;
    protected View topLine;

    protected abstract String getContent();

    public void refresh(Object item) {
        recent = (RecentContact) item;

        updateBackground();

        loadPortrait();

        updateNewIndicator();

        updateNickLabel(MessageHelper.getName(recent.getContactId(), recent.getSessionType()));

        updateMsgLabel();
    }

    public void refreshCurrentItem() {
        if (recent != null) {
            refresh(recent);
        }
    }

    private void updateBackground() {
        topLine.setVisibility(isFirstItem() ? View.GONE : View.VISIBLE);
        bottomLine.setVisibility(isLastItem() ? View.VISIBLE : View.GONE);
        if ((recent.getTag() & RecentContactsFragment.RECENT_TAG_STICKY) == 0) {
            view.setBackgroundResource(R.drawable.list_item_bg_selecter);
        } else {
            view.setBackgroundResource(R.drawable.list_item_bg_sticky_selecter);
        }
    }

    protected void loadPortrait() {
        // 设置头像
        if (recent.getSessionType() == SessionTypeEnum.P2P) {
        	//TODO
        	imgHead.loadBuddyHeadImage(recent.getContactId());
        	TeamMemberX bean = TeamMemberInfoCache.getInstance().getTeammMemberInfo(recent.getContactId());
        	if(bean != null && !TextUtils.isEmpty(bean.getHeadImage())){
//        		Glide.with(context).load(bean.getHeadImage()).into(imgHead);
        		Glide.with(context.getApplicationContext()).load(bean.getHeadImage()).into(imgHead);
        	}
            if(null != Const.contactInfo){
            	imgManger.setVisibility(recent.getContactId().equals(Const.contactInfo.getAccId()) ? View.VISIBLE : View.GONE);
            }
        } else if (recent.getSessionType() == SessionTypeEnum.Team) {
            imgHead.setImageResource(R.drawable.avatar_group);
        }
    }

    private void updateNewIndicator() {
        int unreadNum = recent.getUnreadCount();
        unreadNum = ReminderSettings.unreadMessageShowRule(unreadNum);
        lblUnread.setVisibility(unreadNum > 0 ? View.VISIBLE : View.GONE);
        //unreadIndicator.setVisibility(unreadNum > 0 ? View.VISIBLE : View.GONE);
        lblUnread.setText(String.valueOf(unreadNum));
    }

    private void updateMsgLabel() {
        MoonUtil.identifyFaceExpressionAndTags(context, lblMessage, getContent(), ImageSpan.ALIGN_BOTTOM, 0.45f);
        //lblMessage.setText(getContent());

        MsgStatusEnum status = recent.getMsgStatus();
        switch (status) {
            case fail:
                imgMsgStatus.setImageResource(R.drawable.g_ic_failed_small);
                imgMsgStatus.setVisibility(View.VISIBLE);
                break;
            case sending:
                imgMsgStatus.setImageResource(R.drawable.ic_msg_sending);
                imgMsgStatus.setVisibility(View.VISIBLE);
                break;
            default:
                imgMsgStatus.setVisibility(View.GONE);
                break;
        }
        if(recent.getTime()==0){
        	//说明时间空
        	 lblDatetime.setVisibility(View.GONE);
        }else{
        	
        	String timeString = TimeUtil.getTimeShowString(recent.getTime(), true);
        	lblDatetime.setText(timeString);
        	lblDatetime.setVisibility(View.VISIBLE);
        }

    }

    protected void updateNickLabel(String nick) {
        if (ScreenUtil.screenWidth <= 0) {
            ScreenUtil.GetInfo(NimCache.getContext());
        }

        int labelWidth = ScreenUtil.screenWidth;
        labelWidth -= ScreenUtil.dip2px(50 + 70); // 减去固定的头像和时间宽度

        if (labelWidth > 0) {
            lblNickname.setMaxWidth(labelWidth);
        }

        lblNickname.setText(nick);
    }

    protected int getResId() {
        return R.layout.item_lst_im;
    }

    public void inflate() {
        this.portraitPanel = (FrameLayout) view.findViewById(R.id.portrait_panel);
        this.imgHead = (HeadImageView) view.findViewById(R.id.imgHead);
        this.imgManger = (ImageView) view.findViewById(R.id.imgHead_manger_icon);
        this.lblNickname = (TextView) view.findViewById(R.id.lblNickname);
        this.lblMessage = (TextView) view.findViewById(R.id.lblMessage);
        this.lblUnread = (TextView) view.findViewById(R.id.unread_number_tip);
        this.unreadIndicator = view.findViewById(R.id.new_message_indicator);
        this.lblDatetime = (TextView) view.findViewById(R.id.lblDateTime);
        this.imgMsgStatus = (ImageView) view.findViewById(R.id.imgMsgStatus);
        this.bottomLine = view.findViewById(R.id.bottom_line);
        this.topLine = view.findViewById(R.id.top_line);
    }

    @Override
    public void onClick(View v) {

    }
}
